﻿using System;
using System.Collections.Generic;
using System.Linq;

using System.Data;
using Prodma.DataAccess.Database;
using Prodma.DataAccess.Interfaces.Models;
using Prodma.DataAccess.Models.ViewModels;
using Prodma.DataAccess.Repositories;
using Prodma.DataAccess.Helper.Enumerators;
using System.Configuration;

namespace Prodma.DataAccess.DataService
{
    public class GalleryService : IGalleryRepository
    {

        private DataContext context = new DataContext();


        #region IGalleryRepository Members

        public IList<GalleryVm> GetList(int page, int categoryId)
        {
            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["dataPagerLimit"]);
            int skipValue = page * limitValue;
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from gallery in context.tb_Gallery
                            where gallery.tb_GalleryCategory.Id == categoryId
                            select new GalleryVm()
                            {
                                Id = gallery.Id,
                                Name = gallery.Name,
                                Date = gallery.Date,
                                File = gallery.File,
                                State = new StateVm() { Id = gallery.tb_State.Id, Name = gallery.tb_State.Name }
                            }).OrderBy(x => x.Date).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        public IList<GalleryVm> GetList(int page, int categoryId, Dictionary<string, string> find)
        {
            int limitValue = Convert.ToInt32(ConfigurationManager.AppSettings["dataPagerLimit"]);
            int skipValue = page * limitValue;

            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                var list = (from gallery in context.tb_Gallery
                            where gallery.tb_GalleryCategory.Id == categoryId &&
                                  gallery.Name.StartsWith(findName)
                            select new GalleryVm()
                            {
                                Id = gallery.Id,
                                Name = gallery.Name,
                                Date = gallery.Date,
                                File = gallery.File,
                                State = new StateVm() { Id = gallery.tb_State.Id, Name = gallery.tb_State.Name }
                            }).OrderBy(x => x.Date).Skip(skipValue).Take(limitValue).ToList();

                return list;
            }
        }
        #endregion

        #region IRecordOperation<tb_Gallery,GalleryVm> Members

        public IList<GalleryVm> GetList()
        {
            throw new NotImplementedException();
        }

        public void New(tb_Gallery entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                context.AddTotb_Gallery(entity);
                context.SaveChanges();
            }
        }
        public GalleryVm Edit(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from gallery in context.tb_Gallery
                            where gallery.Id == id
                            select new GalleryVm()
                            {
                                Id = gallery.Id,
                                Name = gallery.Name,
                                Description = gallery.Description,
                                Date = gallery.Date,
                                File = gallery.File,
                                GalleryCategory = new GalleryCategoryVm() { Id = gallery.tb_GalleryCategory.Id, Name = gallery.tb_GalleryCategory.Name },
                                State = new StateVm() { Id = gallery.tb_State.Id, Name = gallery.tb_State.Name }
                            }).FirstOrDefault();

                return edit;
            }
        }
        public void Update(int id, tb_Gallery entity)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var edit = (from gallery in context.tb_Gallery
                            where gallery.Id == id
                            select gallery).FirstOrDefault();

                edit.Name = entity.Name;
                edit.Description = entity.Description;
                edit.Date = entity.Date;
                edit.File = entity.File;
                edit.tb_GalleryCategoryReference.EntityKey = entity.tb_GalleryCategoryReference.EntityKey;
                edit.tb_StateReference.EntityKey = entity.tb_StateReference.EntityKey;

                context.SaveChanges();
            }
        }
        public void Delete(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var delete = (from gallery in context.tb_Gallery
                              where gallery.Id == id
                              select gallery).FirstOrDefault();

                context.DeleteObject(delete);
                context.SaveChanges();
            }
        }
        #endregion

        #region IPageOperation<GalleryVm> Members

        public IList<GalleryVm> GetList(int page)
        {
            throw new NotImplementedException();
        }

        public int GetTotalRowCount()
        {
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from gallery in context.tb_Gallery
                                select gallery).Count();

                return rowCount;
            }
        }
        #endregion

        #region IRowCountOperation Members

        public int GetTotalRowCount(Dictionary<string, string> find)
        {
            string findName = find["name"];
            DataContext context = new DataContext();
            using (context)
            {
                int rowCount = (from gallery in context.tb_Gallery
                                where gallery.Name.StartsWith(findName)
                                select gallery).Count();

                return rowCount;
            }
        }
        #endregion

        #region IStateOperation Members

        public void SetActive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var viewState = (from gallery in context.tb_Gallery
                                 where gallery.Id == id
                                 select gallery).FirstOrDefault();

                viewState.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Active));

                context.SaveChanges();
            }
        }
        public void SetPassive(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var viewState = (from gallery in context.tb_Gallery
                                 where gallery.Id == id
                                 select gallery).FirstOrDefault();

                viewState.tb_StateReference.EntityKey = new EntityKey("DataContext.tb_State", "Id", Convert.ToInt32(State.Passive));

                context.SaveChanges();
            }
        }
        #endregion

        #region IImageOperation Members

        public void RemoveImage(int id)
        {
            DataContext context = new DataContext();
            using (context)
            {
                var removeFile = (from gallery in context.tb_Gallery
                                  where gallery.Id == id
                                  select gallery).FirstOrDefault();

                removeFile.File = ConfigurationManager.AppSettings["galleryBlankImage"].ToString();

                context.SaveChanges();
            }
        }
        #endregion


    }
}
